﻿#include "filehelper.h"
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QRegExp>
FileHelper::FileHelper() {}

QString FileHelper::readAllTxt(const QString& path)
{
    QFile file(path);
    if (file.exists() && file.open(QIODevice::ReadOnly)) {
        QByteArray data = file.readAll();
        file.close();
        return data;
    }
    return QString();
}
QVariant FileHelper::readAllJson(const QString& filePath)
{
    QString       data = FileHelper::readAllTxt(filePath);
    QJsonDocument doc  = QJsonDocument::fromJson(data.toLocal8Bit());
    return doc.toVariant();
}

QString FileHelper::joinPath(const QString& path1, const QString& path2)
{
    QString     path     = path1 + "/" + path2;
    QStringList pathList = path.split(QRegExp("[/\\]"), QString::SkipEmptyParts);
    path                 = pathList.join("/");
    return QDir::cleanPath(path);
}

bool FileHelper::mkPath(const QString& path)
{
    QDir dir;
    return dir.mkpath(path);
}
